﻿using AiMoWindows.CodeScripts.IO;
using System.Drawing;

namespace AiMoWindows.CodeScripts
{
    class ScriptAttributes
    {
        /// <summary>
        /// 默认为创建时有一个GUID，如果Code是Share的，则会赋予一个新ShareKey。如果Code是AskAi的，则会赋予AskAI的
        /// </summary>
        public string Source { get; set; } = InitSourceFromNewCreated;
        /// <summary>
        /// 创建者的UserName。
        /// 登陆了使用UserName，没登陆使用MachineID。
        /// 如果是询问AI获得的代码，则使用AiMo Server返回的UserName
        /// 
        /// 这样就可以追溯修改代码的人。
        /// 
        /// 如果是Download和Upload代码，则会保持原来的创建者
        /// </summary>
        public string UserName { get; set; } = InitUserName();
        /// <summary>
        /// 询问了才有Question
        /// </summary>
        public string Question { get; set; } = "";
        /// <summary>
        /// 创建者的RhinoVersion
        /// </summary>
        public string RhinoVersion { get; set; } = Server.ServerHelper.RhinoVersion;
        public string CodeName { get; set; } = "";
        public Bitmap? ScreenShot { get; set; }
        string _Description = "";
        /// <summary>
        /// 没有Comment // Mark
        /// </summary>
        public string Description
        {
            get => _Description;
            set => _Description = CodeIO.CompatibleRemoveLineCommentSlash(value);
        }

        string _Author = "";
        /// <summary>
        ///  没有Comment // Mark
        /// </summary>
        public string Author
        {
            get => _Author;
            set => _Author = CodeIO.CompatibleRemoveLineCommentSlash(value);
        }

        static string InitSourceFromNewCreated => $"New_[{DateTime.Now}]_{Guid.NewGuid().ToString()[24..]}";
        public void InitSourceFromAI(string userName, string question)
        {
            Source = $"Ai_[{DateTime.Now}]_{Guid.NewGuid().ToString()[24..]}";
            UserName = userName;
            Question = question;
        }

        public void SetSourceFromShareKey(string shareKey)
        {
            Source = $"Download_[{DateTime.Now}]_{shareKey}";
        }

        public static string InitUserName()
        {
            if (Server.ServerHelper.LogInAsCommunityUser)
            {
                return Server.ServerHelper.UserName;
            }
            else
            {
                string id = Server.ServerHelper.MachineID;
                if (string.IsNullOrEmpty(id)) return "";
                return id;
            }
        }
    }

}
